package top.huhuiyu.springboot2.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huhuiyu.springboot2.entity.*;
import top.huhuiyu.springboot2.service.NewsService;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
    private final Logger logger = LoggerFactory.getLogger(NewsController.class);

    @Autowired
    private NewsService newsService;

    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable("id") Integer id) {
        return newsService.delete(id);
    }

    @PutMapping("")
    public BaseResult modify(@RequestBody TbNews tbNews) {
        return newsService.modify(tbNews);
    }

    // get /news 查询集合
    // get /news/1 查询主键
    @GetMapping("/{id}")
    public BaseDataResult<TbNews> queryByKey(@PathVariable("id") Integer id) {
        System.out.println("地址栏传入的id值:" + id);
        return newsService.queryByKey(id);
    }

    @PostMapping("")
    public BaseResult add(@RequestBody TbNews news) {
        //@RequestBody表示参数是使用json格式传入
        return newsService.add(news);
    }

    // get查询，post添加，put修改，delete删除，patch局部修改
    @GetMapping("")
    public BaseDataResult<PageResult<List<TbNews>>> query(NewsQuery query, PageBean page) {
        logger.debug("请求参数信息:{},{}", query, page);
        return newsService.query(query, page);
    }
}
